﻿using System.Collections.Generic;
using System.Windows.Media;

using MSAP.Framework.Interfaces;

namespace MSAP.Framework
{
    public class ModuleBase : IModule
    {
        #region IModule Members

        private ModuleBase() 
        {
            // Pas d'instance sans parametre
        }
        
        protected ModuleBase(string id, string title, string comments, ImageSource icon)
        {
            this.m_ID = id;
            this.m_Title = title;
            this.m_Comments = comments;
            this.m_Icon = icon;
        }

        protected ModuleBase(string id, string title, string comments)
        {
            this.m_ID = id;
            this.m_Title = title;
            this.m_Comments = comments;
            this.m_Icon = null;
        }

        public virtual void Init(IShell shell)
        {
            m_Shell = shell;
            m_ServiceActions = new List<IServiceAction>(); 
            m_Workplaces = new List<IServicePage>();
        }

        public IShell Shell
        {
            get { return m_Shell; }
        }

        public string ID
        {
            get { return m_ID; }
        }

        public string Title
        {
            get { return m_Title; }
        }

        public string Comments
        {
            get { return m_Comments; }
        }

        public ImageSource Icon
        {
            get { return m_Icon; }
        }

        public List<IServiceAction> ServiceActions
        {
            get { return m_ServiceActions; }
        }

        public List<IServicePage> ServicePages
        {
            get { return m_Workplaces; }
        }

        public string RolesAccess
        {
            get { return m_RolesAccess; }
            set { m_RolesAccess = value; }
        }

        public bool CheckAccessForRole(string sRoleName)
        {
            if (string.IsNullOrEmpty(RolesAccess) == true) return true; // pas de role access donc par dfaut on a accés au module

            // rechercher dans la liste des roles access, si le role de l'utilisateur est present.
            // dans le cas ou le role de l'utilisateur est present, il a access à la fonction
            // Par defaut, il n'a pas acces
            string[] roleNames = RolesAccess.Split(',');
            foreach (string role in roleNames)
            {
                if (role.CompareTo(sRoleName) == 0)
                    return true;
            }

            return false;
        }

        public virtual void AddServicePage(IServicePage service)
        {
            ServicePages.Add(service);
            Shell.AddServicePage(service);
        }
        
        public virtual void MoveToServicePage(IServicePage service)
        {
            Shell.MoveToServicePage(service);
        }

        public virtual void RemoveServicePage(IServicePage service)
        {
            ServicePages.Remove(service);
            Shell.RemoveServicePage(service);
        }

        public virtual bool CanRemoveServicePage(IServicePage service)
        {
            return Shell.CanRemoveServicePage(service);
        }

        public virtual void RemoveAllServicePage()
        {
            int countWorkplaces = ServicePages.Count;
            for (int i = countWorkplaces - 1; i >= 0; i--)
            {
                RemoveServicePage(ServicePages[i]);
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region private fields
        protected IShell m_Shell;
        protected string m_ID;
        protected string m_Title;
        protected string m_Comments;
        protected ImageSource m_Icon;
        protected List<IServiceAction> m_ServiceActions;
        protected List<IServicePage> m_Workplaces;
        protected string m_RolesAccess;
        #endregion

    }
}
